package com.gx.smart.building.ui.common.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseViewModel;
import com.gx.smart.base.livedata.SingleLiveEvent;
import com.gx.smart.building.ui.common.model.UploadImage;
import com.gx.smart.lib.http.api.RetrofitManager;
import com.gx.smart.lib.http.api.UploadFile;
import com.orhanobut.logger.Logger;

import java.util.List;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * @author: xiaosy
 * 时间: 2022/3/30
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class CommonViewModel extends BaseViewModel {
    public SingleLiveEvent<UploadImage> uploadSuccess = new SingleLiveEvent<>();
    public SingleLiveEvent<UploadImage> uploadAudioSuccess = new SingleLiveEvent<>();
    public SingleLiveEvent<UploadImage> adapterUploadSuccess = new SingleLiveEvent<>();

    public CommonViewModel(@NonNull Application application) {
        super(application);
    }

    public void uploadImage(String fileName, byte[] file) {
        //构建要上传的文件
        //1.创建MultipartBody.Builder对象
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        //2.获取图片，创建请求体
        RequestBody body = RequestBody.create(file);//表单类型

//3.调用MultipartBody.Builder的addFormDataPart()方法添加表单数据
        builder.addFormDataPart("uploadType", "3");
        builder.addFormDataPart("type", "1");
        builder.addFormDataPart("businessTag", "rj-public");
        builder.addFormDataPart("subPaths", "image");
        builder.addFormDataPart("uploadFileName", fileName);
        builder.addFormDataPart("file", fileName, body);

//4.创建List<MultipartBody.Part> 集合，
//  调用MultipartBody.Builder的build()方法会返回一个新创建的MultipartBody
//  再调用MultipartBody的parts()方法返回MultipartBody.Part集合
        List<MultipartBody.Part> parts = builder.build().parts();

        Call<UploadFile> call = RetrofitManager.getInstance().getCommonService().uploadFile(parts);

        call.enqueue(new Callback<UploadFile>() {
            @Override
            public void onResponse(Call<UploadFile> call, retrofit2.Response<UploadFile> response) {
                UploadFile result = response.body();
                UploadImage uploadImage = new UploadImage();
                if (result == null) {
                    error.setValue(true);
                    uploadImage.setSuccess(false);
                    uploadImage.setFileName(fileName);
                    uploadSuccess.setValue(uploadImage);
                    ToastUtils.showLong("网络请求超时");
                    return;
                }
                if (result.getCode() == 100) {
                    uploadImage.setSuccess(true);
                    uploadImage.setFileName(fileName);
                    uploadImage.setPath(result.getSubUrl());
                    uploadSuccess.setValue(uploadImage);
                } else {
                    error.setValue(true);
                    uploadImage.setSuccess(false);
                    uploadImage.setFileName(fileName);
                    uploadSuccess.setValue(uploadImage);
                    ToastUtils.showLong(result.getMsg());
                }
            }

            @Override
            public void onFailure(Call<UploadFile> call, Throwable t) {
                Logger.e(t.getMessage());
                error.setValue(true);
                UploadImage uploadImage = new UploadImage();
                uploadImage.setSuccess(false);
                uploadImage.setFileName(fileName);
                uploadSuccess.setValue(uploadImage);
                ToastUtils.showLong("网络请求异常");
            }
        });

    }

    public void uploadAdapterImage(String fileName, byte[] file) {
        //构建要上传的文件
        //1.创建MultipartBody.Builder对象
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        //2.获取图片，创建请求体
        RequestBody body = RequestBody.create(file);//表单类型

        //3.调用MultipartBody.Builder的addFormDataPart()方法添加表单数据
        builder.addFormDataPart("uploadType", "3");
        builder.addFormDataPart("type", "1");
        builder.addFormDataPart("businessTag", "rj-public");
        builder.addFormDataPart("subPaths", "image");
        builder.addFormDataPart("uploadFileName", fileName);
        builder.addFormDataPart("file", fileName, body);

//4.创建List<MultipartBody.Part> 集合，
//  调用MultipartBody.Builder的build()方法会返回一个新创建的MultipartBody
//  再调用MultipartBody的parts()方法返回MultipartBody.Part集合
        List<MultipartBody.Part> parts = builder.build().parts();

        Call<UploadFile> call = RetrofitManager.getInstance().getCommonService().uploadFile(parts);

        call.enqueue(new Callback<UploadFile>() {
            @Override
            public void onResponse(Call<UploadFile> call, retrofit2.Response<UploadFile> response) {
                UploadFile result = response.body();
                UploadImage uploadImage = new UploadImage();
                if (result == null) {
                    error.setValue(true);
                    uploadImage.setSuccess(false);
                    uploadImage.setFileName(fileName);
                    adapterUploadSuccess.setValue(uploadImage);
                    ToastUtils.showLong("网络请求超时");
                    return;
                }
                if (result.getCode() == 100) {
                    uploadImage.setSuccess(true);
                    uploadImage.setFileName(fileName);
                    uploadImage.setPath(result.getSubUrl());
                    adapterUploadSuccess.setValue(uploadImage);
                } else {
                    error.setValue(true);
                    uploadImage.setSuccess(false);
                    uploadImage.setFileName(fileName);
                    adapterUploadSuccess.setValue(uploadImage);
                    ToastUtils.showLong(result.getMsg());
                }
            }

            @Override
            public void onFailure(Call<UploadFile> call, Throwable t) {
                Logger.e(t.getMessage());
                error.setValue(true);
                UploadImage uploadImage = new UploadImage();
                uploadImage.setSuccess(false);
                uploadImage.setFileName(fileName);
                adapterUploadSuccess.setValue(uploadImage);
                ToastUtils.showLong("网络请求异常");
            }
        });

    }


    public void uploadAudio(String fileName, byte[] file) {
        //构建要上传的文件
        //1.创建MultipartBody.Builder对象
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        //2.获取图片，创建请求体
        RequestBody body = RequestBody.create(file);//表单类型

//3.调用MultipartBody.Builder的addFormDataPart()方法添加表单数据
        builder.addFormDataPart("uploadType", "3");
        builder.addFormDataPart("type", "1");
        builder.addFormDataPart("businessTag", "rj-public");
        builder.addFormDataPart("subPaths", "audio");
        builder.addFormDataPart("uploadFileName", fileName);
        builder.addFormDataPart("file", fileName, body);

//4.创建List<MultipartBody.Part> 集合，
//  调用MultipartBody.Builder的build()方法会返回一个新创建的MultipartBody
//  再调用MultipartBody的parts()方法返回MultipartBody.Part集合
        List<MultipartBody.Part> parts = builder.build().parts();

        Call<UploadFile> call = RetrofitManager.getInstance().getCommonService().uploadFile(parts);
        call.enqueue(new Callback<UploadFile>() {
            @Override
            public void onResponse(Call<UploadFile> call, retrofit2.Response<UploadFile> response) {
                UploadFile result = response.body();
                UploadImage uploadImage = new UploadImage();
                if (result == null) {
                    error.setValue(true);
                    uploadImage.setSuccess(false);
                    uploadAudioSuccess.setValue(uploadImage);
                    ToastUtils.showLong("网络请求超时");
                    return;
                }
                if (result.getCode() == 100) {
                    uploadImage.setSuccess(true);
                    uploadImage.setFileName(fileName);
                    uploadImage.setPath(result.getSubUrl());
                    uploadAudioSuccess.setValue(uploadImage);
                } else {
                    error.setValue(true);
                    uploadImage.setSuccess(false);
                    uploadAudioSuccess.setValue(uploadImage);
                    ToastUtils.showLong(result.getMsg());
                }
            }

            @Override
            public void onFailure(Call<UploadFile> call, Throwable t) {
                Logger.e(t.getMessage());
                error.setValue(true);
                UploadImage uploadImage = new UploadImage();
                uploadImage.setSuccess(false);
                uploadImage.setFileName(fileName);
                uploadAudioSuccess.setValue(uploadImage);
                ToastUtils.showLong("网络请求异常");
            }
        });

    }
}
